package cn.edu.zjicm.wordsnet_d.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;

public class AppRecommendActivity extends WordBaseActivity {

	private WebView webView;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_recommend);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("应用推荐");

		webView = (WebView) findViewById(R.id.app_recommend_web_view);

		// 使连接不以浏览器方式打开
		webView.setWebViewClient(webViewClient);
		webView.addJavascriptInterface(new MyJavascriptInterface(), "android");
		webView.requestFocus();
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setAppCacheEnabled(false);

		loadUrl();
	}

	private void loadUrl() {
		webView.loadUrl("http://www.iwordnet.com/mobile/tuijian/app_tj.html");
		progressDialog = ProgressDialog.show(AppRecommendActivity.this, null, "加载中，请稍候...");
	}

	WebViewClient webViewClient = new WebViewClient() {

		public void onPageFinished(WebView view, String url) {
			progressDialog.dismiss();
		};

		// public boolean shouldOverrideUrlLoading(WebView view, String url) {
		// LogUtil.d("Tag", "调用shouldOverrideUrlLoading");
		// view.loadUrl(url);
		// return false;
		// };
	};
	
	class MyJavascriptInterface {

		// 注意这里的参数要设置成final
		// android4.2以后需要这个注解
		@android.webkit.JavascriptInterface
		public void downloadApk(final String url) {
			Uri uri = Uri.parse(url);
			Intent in = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(in);
		}
	}
}
